package blog.gui;

import javax.microedition.lcdui.*;

import blog.*;
import blog.controller.*;
import blog.util.StringUtils;

public class HtmlInsertLinkGui extends Form implements IBaseGui, CommandListener, ItemStateListener {
	
	private static Command cmdBack = new Command( ConstString.CMD_BACK, Command.BACK, 0 );
	private static Command cmdOk = new Command( ConstString.CMD_INSERT_INPUT_ONLY, Command.OK, 0 );
	
	TextField url;
	TextField text;
	ChoiceGroup followTag;
	
	StringItem showResult;
	
	int insertPos;
	
	public HtmlInsertLinkGui( int insertPos )
	{
		super( ConstString.GUI_TITLE_HTML_INSERT_LINK_GUI );
		url = new TextField( ConstString.GUI_HTML_INSERT_LINK_GUI_URL, "http://", 200, TextField.URL );
		text = new TextField( ConstString.GUI_HTML_INSERT_LINK_GUI_TEXT, "", 50, TextField.ANY );
		followTag = new ChoiceGroup("", Choice.MULTIPLE );
		followTag.append(ConstString.GUI_HTML_INSERT_LINK_GUI_FOLLOW_TAG, null);
		
		showResult = new StringItem(ConstString.GUI_HTML_INSERT_LINK_GUI_SHOW_RESULT_TITLE, calculateResult());
		
		append(url);
		append(text);
		append(followTag);
		append(showResult);
		
		addCommand(cmdBack);
		addCommand(cmdOk);
		setCommandListener(this);
		
		setItemStateListener(this);
		
		this.insertPos = insertPos;
	}
	
	String calculateResult()
	{
		String param1 = url.getString();
		String param2 = (followTag.isSelected(0))?ConstString.GUI_HTML_INSERT_LINK_GUI_FOLLOW_TAG_ON:"";
		String param3 = text.getString();
		
		return StringUtils.combin(ConstString.GUI_HTML_INSERT_LINK_GUI_SHOW_RESULT, new String[]{param1, param2, param3});
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_HTML_INSERT_LINK;
	}

	public void notifyChanged(Object param) {

	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==cmdBack )
		{
			SysRuntime.guiManager.back();
		}
		else if ( cmd==cmdOk )
		{
			AdvanceBodyEdit.getInstance().insertSomething(showResult.getText(), insertPos);
			SysRuntime.guiManager.back();
		}
	}

	public void itemStateChanged(Item item) {
		if ( item==url || item==text || item==followTag )
		{
			showResult.setText(calculateResult());
		}
	}

}